import Cookies from 'js-cookie'
import { getLanguage } from '@/lang/index'
import { getBtn, setBtn } from '@/utils/auth'
// import local from '@/views/i18n-demo/local'
const projectName = 'mall-front-tenant'
const state = {
  sidebar: {
    opened: Cookies.get('sidebarStatus') ? !!+Cookies.get('sidebarStatus') : true,
    withoutAnimation: false
  },
  controlBtn: getBtn(),
  device: 'desktop',
  language: getLanguage(),
  lableWidth: '120px',
  size: Cookies.get('size') || 'medium',
  menuCheck: Boolean(localStorage.getItem(`${projectName}-menuCheck`)) || false,
  menuFirstIndex: localStorage.getItem(`${projectName}-menuFirstIndex`) || 0,
  navOneIndex: localStorage.getItem(`${projectName}-navOneIndex`) || 0,
  navTwoIndex: localStorage.getItem(`${projectName}-navTwoIndex`) || 0
}

const mutations = {
  SET_BTN: (state, arr) => {
    state.controlBtn = arr
  },
  TOGGLE_SIDEBAR: state => {
    state.sidebar.opened = !state.sidebar.opened
    state.sidebar.withoutAnimation = false
    if (state.sidebar.opened) {
      Cookies.set('sidebarStatus', 1)
    } else {
      Cookies.set('sidebarStatus', 0)
    }
  },
  CLOSE_SIDEBAR: (state, withoutAnimation) => {
    Cookies.set('sidebarStatus', 0)
    state.sidebar.opened = false
    state.sidebar.withoutAnimation = withoutAnimation
  },
  TOGGLE_DEVICE: (state, device) => {
    state.device = device
  },
  SET_LANGUAGE: (state, language) => {
    state.language = language
    Cookies.set('language', language)
  },
  SET_SIZE: (state, size) => {
    state.size = size
    Cookies.set('size', size)
  },
  SET_MENUCHECK: (state, bool) => {
    state.menuCheck = bool
    localStorage.setItem(`${projectName}-menuCheck`, bool)
  },
  SET_MENUFIRSTINDEX: (state, index) => {
    state.menuFirstIndex = index
    localStorage.setItem(`${projectName}-menuFirstIndex`, index)
  },
  NAV_ONE: (state, index) => {
    state.navOneIndex = index
    localStorage.setItem(`${projectName}-navOneIndex`, index)
  },
  NAV_TWO: (state, index) => {
    state.navTwoIndex = index
    localStorage.setItem(`${projectName}-navTwoIndex`, index)
  }
}

const actions = {
  setBtn({ commit }, arr) {
    setBtn(arr)
    commit('SET_BTN', arr)
  },
  toggleSideBar({ commit }) {
    commit('TOGGLE_SIDEBAR')
  },
  closeSideBar({ commit }, { withoutAnimation }) {
    commit('CLOSE_SIDEBAR', withoutAnimation)
  },
  toggleDevice({ commit }, device) {
    commit('TOGGLE_DEVICE', device)
  },
  setLanguage({ commit }, language) {
    commit('SET_LANGUAGE', language)
  },
  setSize({ commit }, size) {
    commit('SET_SIZE', size)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
